

#import "MailListCell.h"
static UIImage *attachmentImage=nil;
static UIImage *unreadImage=nil;

@interface MailListCell()
- (UILabel *)newLabelWithPrimaryColor:(UIColor *)primaryColor selectedColor:(UIColor *)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold;
@end

@implementation MailListCell

@synthesize mailSenderLabel = _mailSenderLabel;
@synthesize mailDateLabel = _mailDateLabel;
@synthesize mailSubjectLabel = _mailSubjectLabel;
@synthesize attachmentImageView=_attachmentImageView;
@synthesize readStatusView=_readStatusView;

+ (void)initialize
{
	attachmentImage=[[UIImage imageNamed:@"MailAttachmentSign.png"] retain];
	unreadImage=[[UIImage imageNamed:@"MailUnreadSign.png"] retain];

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView *myContentView = self.contentView;
        
		self.attachmentImageView=[[UIImageView alloc] initWithImage:attachmentImage];
		[myContentView addSubview:self.attachmentImageView];
		[self.attachmentImageView release];
		
		self.readStatusView=[[UIImageView alloc] initWithImage:unreadImage];
		[myContentView addSubview:self.readStatusView];
		[self.readStatusView release];
        
        // A label that displays the location of the earthquake.
        self.mailSenderLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:16.0 bold:YES]; 
		self.mailSenderLabel.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.mailSenderLabel];
		[self.mailSenderLabel release];
        
        // A label that displays the date of the earthquake.
        self.mailDateLabel = [self newLabelWithPrimaryColor:[UIColor blueColor] selectedColor:[UIColor lightGrayColor] fontSize:14.0 bold:NO];
		self.mailDateLabel.textAlignment = UITextAlignmentRight; // default
		[myContentView addSubview:self.mailDateLabel];
		[self.mailDateLabel release];
        
        // A label that displays the magnitude of the earthquake.
        self.mailSubjectLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:14.0 bold:NO];
		self.mailSubjectLabel.textAlignment = UITextAlignmentLeft;
		[myContentView addSubview:self.mailSubjectLabel];
		[self.mailSubjectLabel release];
		
        
        // Position the magnitudeImageView above all of the other views so
        // it's not obscured. It's a transparent image, so any views
        // that overlap it will still be visible.
		[myContentView bringSubviewToFront:self.attachmentImageView];
		[myContentView bringSubviewToFront:self.readStatusView];
    }
    return self;
}

- (Mail *)mail
{
    return _mail;
}


- (void)setMail:(Mail *)newMail
{
    [newMail retain];
    [_mail release];
    _mail = newMail;	
	if(newMail.sender!=nil)
		self.mailSenderLabel.text = newMail.sender;
	else
		self.mailSenderLabel.text = @"";
	NSMutableString *convertDate=[[NSMutableString alloc] init];
	if(newMail.date!=nil ){
		int dayTime=60*24*60;
		NSDateFormatter* transfer=[[NSDateFormatter alloc] init];
		[transfer setDateFormat:@"d:M:y"];
		NSString *formattedDateString = [transfer stringFromDate:[NSDate date]];
		NSDate *todayOffset=[transfer dateFromString:formattedDateString];
		NSTimeInterval interval=[todayOffset timeIntervalSinceDate:newMail.date]+dayTime; 
		NSDateFormatter *outputFormatter=[[NSDateFormatter alloc] init];
		if(interval<dayTime){
			[outputFormatter setDateFormat:@"H:mm a"]; 
		}
		else if(interval<(dayTime*7)){
			[outputFormatter setDateFormat:@"EEEE"]; 
		}
		else {
			[outputFormatter setDateFormat:@"d/M/y"]; 
		}
	
		[convertDate setString:[outputFormatter stringFromDate:	newMail.date]];	
		[transfer release];
		[outputFormatter release];
	}
	self.mailDateLabel.text = convertDate;
	if(newMail.subject!=nil)
		self.mailSubjectLabel.text = newMail.subject;
	else
		self.mailSubjectLabel.text=@"";
	if(![newMail hasAttachment])
		self.attachmentImageView.image=nil;
	else
		self.attachmentImageView.image=attachmentImage;
	if(![newMail hasRead])
		self.readStatusView.image=unreadImage;
	else
		self.readStatusView.image=nil;
    [self setNeedsDisplay];
	[convertDate release];
	
}




- (void)layoutSubviews {
    
#define SENDER_WIDTH 170
#define	DATE_WIDTH 90
#define	SUBJECT_WIDTH 270
#define COLUMN_OFFSET 5
#define ROW_HEIGHT 20	
#define UPPER_ROW_OFFSET 6
#define LOW_ROW_OFFSET 28
    
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
	
	// In this example we will never be editing, but this illustrates the appropriate pattern
	if (!self.editing) {
		
        CGFloat boundsX = contentRect.origin.x + 20;
		CGRect frame;
        
		UIImageView *imageView = self.readStatusView;
        frame = [imageView frame];
		frame.origin.x = 2;
		frame.origin.y = 16;
 		imageView.frame = frame;
		
        // Place the sender label.
		frame = CGRectMake(boundsX + COLUMN_OFFSET, UPPER_ROW_OFFSET, SENDER_WIDTH, ROW_HEIGHT);
		self.mailSenderLabel.frame = frame;
        
        // Place the date label.
		frame = CGRectMake(boundsX + 2*COLUMN_OFFSET+SENDER_WIDTH, UPPER_ROW_OFFSET, DATE_WIDTH, ROW_HEIGHT);
		self.mailDateLabel.frame = frame;
        
        // Place the Subject label.
        frame = CGRectMake(boundsX + COLUMN_OFFSET, LOW_ROW_OFFSET, SUBJECT_WIDTH, ROW_HEIGHT);
        self.mailSubjectLabel.frame = frame;
		
		imageView = self.attachmentImageView;
        frame = [imageView frame];
		frame.origin.x = boundsX + COLUMN_OFFSET+SENDER_WIDTH;
		frame.origin.y = UPPER_ROW_OFFSET;
 		imageView.frame = frame;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
	/*
	 Views are drawn most efficiently when they are opaque and do not have a clear background, so in newLabelForMainText: the labels are made opaque and given a white background.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  
     */
	[super setSelected:selected animated:animated];
	
	UIColor *backgroundColor = nil;
	if (selected) {
	    backgroundColor = [UIColor clearColor];
	} else {
		backgroundColor = [UIColor whiteColor];
	}
    
	self.mailSenderLabel.backgroundColor = backgroundColor;
	self.mailSenderLabel.highlighted = selected;
	self.mailSenderLabel.opaque = !selected;
	
	self.mailDateLabel.backgroundColor = backgroundColor;
	self.mailDateLabel.highlighted = selected;
	self.mailDateLabel.opaque = !selected;
	
	self.mailSubjectLabel.backgroundColor = backgroundColor;
	self.mailSubjectLabel.highlighted = selected;
	self.mailSubjectLabel.opaque = !selected;
}

- (UILabel *)newLabelWithPrimaryColor:(UIColor *)primaryColor selectedColor:(UIColor *)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold
{
	/*
        Create and configure a label.
    */

    UIFont *font;
    if (bold) {
        font = [UIFont boldSystemFontOfSize:fontSize];
    } else {
        font = [UIFont systemFontOfSize:fontSize];
    }
    
    /*
        Views are drawn most efficiently when they are opaque and do not have a clear background, so set these defaults.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  This is handled in setSelected:animated:.
    */
	UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
	newLabel.backgroundColor = [UIColor whiteColor];
	newLabel.opaque = YES;
	newLabel.textColor = primaryColor;
	newLabel.highlightedTextColor = selectedColor;
	newLabel.font = font;
	
	return newLabel;
}



-(void) setCellRead
{
	self.readStatusView.image=nil;

}



@end
